from datetime import datetime
import re
from typing import List
from unittest import result
from app.api.admin.model.user import UserModel
from app.api.admin.schemas.user import UserInSchema, UserOutSchema, UserSearchSchema

from app.dependencies.init_databases import db
from sqlalchemy import insert, select





class UserService():

    async def edit(self, **kwargs):
        # print("user service edit")
        user = UserModel()
        for key in kwargs.keys():
            if hasattr(user, key):
                setattr(user, key, kwargs[key])
        await db.execute(
            UserModel.__table__.insert().values(**kwargs)
        )
        print(UserModel.__table__)
        return

    async def create(self, user_in: UserInSchema):
        user = user_in.model_dump()
        user.setdefault("created_time", datetime.now())
        result = await db.execute(insert(UserModel).values(**user))
        print(result)
        return {"msg": "success"}

    async def get_list(self, params: UserSearchSchema):
        print("user service get list")
        where_params = [UserModel.is_deleted == 0]
        query = select(UserModel).where(*where_params)
        results = await db.fetch_all(query)
        # print([type(result) for result in results])
        # print([UserOutSchema.model_validate(result) for result in results])
        
        return await db.fetch_all(query)

    async def get_all(self, params: UserSearchSchema):
        print("user service get one by params")
        return

    async def detail(self, params: UserSearchSchema):
        print("user service get one by params")
        return

    async def remove(self, user_id: int):
        pass

    @classmethod
    def instence(cls):
        '''实例化'''
        return cls()
